package com.starblink.login.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Matrix;
import android.graphics.Outline;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewOutlineProvider;
import android.widget.Button;

import com.starblink.android.common.R;

import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.RequiresApi;
import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatImageButton;
import androidx.appcompat.widget.AppCompatImageView;


/**
 * 用于XML中的ImageView 一般用于展示用户头像等，可以设置边框宽度和颜色
 */
public class CustomLoginBtnView extends AppCompatButton {


    boolean isCenter = false;
    float mPaddingLeft = 0F;
    private static final boolean DEFAULT_BORDER_OVERLAY = false;
    private static final int DEFAULT_BORDER_WIDTH = 0;

    public CustomLoginBtnView(Context context) {
        super(context);
    }

    public CustomLoginBtnView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomLoginBtnView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomerLoginBtnView, defStyle, 0);
        isCenter = a.getBoolean(R.styleable.CustomerLoginBtnView_clb_center, DEFAULT_BORDER_OVERLAY);
        mPaddingLeft = a.getDimensionPixelSize(R.styleable.CustomerLoginBtnView_clb_paddingLeft, DEFAULT_BORDER_WIDTH);

        a.recycle();

    }


    @Override
    protected void onDraw(Canvas canvas) {
        Drawable[] drawables = getCompoundDrawables();

        if (drawables != null) {
            if (isCenter) {
                Drawable drawableLeft = drawables[0];
                if (drawableLeft != null) {
                    float textWidth = getPaint().measureText(getText().toString());
                    int drawablePadding = getCompoundDrawablePadding();
                    int drawableWidth = drawableLeft.getIntrinsicWidth();
                    float bodyWidth = textWidth + drawableWidth + drawablePadding;
                    canvas.translate((getWidth() - bodyWidth) / 2, 0);
                }
            } else if (mPaddingLeft > 0) {
                canvas.translate(mPaddingLeft, 0);
            }

        }

        super.onDraw(canvas);

    }


}
